package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.fragment.FMRepositoryInOutNote;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/14/21
 * <br/>出入库统计
 */
public class ACRepositoryInOutNote extends BaseActivity{


    public static void start(Context context) {
        Intent starter = new Intent(context, ACRepositoryInOutNote.class);
        context.startActivity(starter);
    }


    private TabLayout tabLayout;

    private ViewPager viewPager;

    private final StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);

    private final StyleSpan normalSpan = new StyleSpan(Typeface.NORMAL);

    private final RelativeSizeSpan smallSize = new RelativeSizeSpan(0.88f);

    private final SpannableString inBold = new SpannableString("入库记录");
    private final SpannableString inNormal = new SpannableString("入库记录");


    private final SpannableString outBold = new SpannableString("出库记录");
    private final SpannableString outNormal = new SpannableString("出库记录");

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_repository_in_out_note);

        inBold.setSpan(boldSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        inNormal.setSpan(normalSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        inNormal.setSpan(smallSize, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        outBold.setSpan(boldSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        outNormal.setSpan(normalSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        outNormal.setSpan(smallSize, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("统计");
        simpleHead.setOnItemclicklistener(this::finish);
        tabLayout = ZViewHelp.findById(this,R.id.tabLayout);

        createTab(0);

        viewPager =ZViewHelp.findById(this,R.id.viewPager);

        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                if(tab.getPosition() == 0){

                    tab.setText(inBold);

                }else {

                    tab.setText(outBold);

                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {


                if(tab.getPosition() == 0){

                    tab.setText(inNormal);

                }else {
                    tab.setText(outNormal);

                }

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    }

    //<editor-fold defaultState="collapsed" desc="私有方法">

    private void createTab(int selected){

        TabLayout.Tab tab1 = tabLayout.newTab();
        TabLayout.Tab tab2 = tabLayout.newTab();

        if(selected == 0){


            tab1.setText(inBold);

            tab2.setText("出库记录");

        }else {

            tab1.setText("入库记录");

            tab2.setText(outBold);

        }

        tabLayout.addTab(tab1);
        tabLayout.addTab(tab2);


    }


    // </editor-fold>


    public class MyPagerAdapter extends FragmentPagerAdapter{

        List<Fragment> pager = new ArrayList<>();

        public MyPagerAdapter(@NonNull FragmentManager fm) {
            super(fm,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            pager.add(new FMRepositoryInOutNote());
            pager.add(new FMRepositoryInOutNote());
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return pager.get(position);
        }

        @Override
        public int getCount() {
            return pager.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return position==0?"入库记录":"出库记录";
        }
    }


}
